%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load_str = [main_str,'\Replication_code_RFS\Model\Output\Last'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sel_j_vec = 9; % select \sigma_{\psi} = 9\%
n_j       = size(sel_j_vec,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FontSize=18;
MarkSize=9;
XlabelSize = 16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Load

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load([load_str,'\Results_out_4_Nash_Loop.mat']);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_vec       = start_bid_scale-1; % vector of values of alpha

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure for calibration with \sigma_{\psi} = 9%

for j = 1:n_j

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure(7);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    eq_Erev_vec      = eq_Erev_mat(:,sel_j_vec(j));   
    eq_EbidW_vec     = eq_EbidW_mat(:,sel_j_vec(j));
    eq_EbidW_obj_vec = eq_EbidW_obj_mat(:,sel_j_vec(j));
    eq_Esales_vec    = eq_Esales_mat(:,sel_j_vec(j));
    eq_Psales_vec    = eq_Psales_mat(:,sel_j_vec(j));
    eq_Nall_vec      = eq_Nall_mat(:,sel_j_vec(j));
    eq_EbidN_vec     = eq_EbidN_mat(:,sel_j_vec(j));
    eq_p_hcost0_vec  = eq_p_hcost0_mat(:,sel_j_vec(j));
    eq_OverbidL_vec  = eq_OverbidL_mat(:,sel_j_vec(j));
    eq_WinM_vec      = eq_WinM_mat(:,sel_j_vec(j));

    %%%%%%%%%%%%%%%
    subplot(4,2,1);
    %%%%%%%%%%%%%%%
    temp_max=NaN(length(eq_Erev_vec),1); temp_max(eq_Erev_vec==max(eq_Erev_vec))=max(eq_Erev_vec);
    temp_eq_Erev_vec=eq_Erev_vec; temp_eq_Erev_vec(eq_Erev_vec==max(eq_Erev_vec))=NaN;
    plot(q_vec,temp_eq_Erev_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    plot(q_vec,temp_max/1000,'*','MarkerSize',MarkSize,'MarkerFaceColor','red'); grid on; 
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    title('E[Seller Revenue]')
    ylabel('AUD 1,000');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(4,2,2);
    %%%%%%%%%%%%%%%
    plot(q_vec,eq_EbidW_obj_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;    
    plot(q_vec,eq_EbidW_vec/1000,'Linestyle','none','marker','s','MarkerSize',MarkSize,'MarkerFaceColor','red'); grid on;    
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('AUD 1,000');
    title('E[Bidder Surplus]'); legend({'Objective','Subjective'});
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize); 
    %%%%%%%%%%%%%%%    
    subplot(4,2,3);
    %%%%%%%%%%%%%%%
    plot(q_vec,(eq_Esales_vec/1000)+650,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on; 
    ylabel('AUD 1,000');
    title('E[Sale Price|Sale]')
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%% 
    subplot(4,2,4);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_Psales_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('P(Sale)');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(4,2,5);
    %%%%%%%%%%%%%%%
    plot(q_vec,eq_Nall_vec.*eq_EbidN_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    title('E[Number of Bidders]');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(4,2,6);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_EbidN_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('E[Fraction of Bidders]');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(4,2,7);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_OverbidL_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('P(Winning Bidder Pays Above Objective Private Value)');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(4,2,8);
    %%%%%%%%%%%%%%%
    plot(q_vec,-eq_WinM_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('AUD 1,000');
    title('E[Sales Price - Objective Private Value] for Winning Bidder');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Find Equilibrium for Different Values of \sigma_{\psi}, from model in which bidders maximize ``subjective'' surplus (including valuation shocks)
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_opt_vec            = nan(J,1);
eq_Erev_opt_vec      = nan(J,1);
eq_EbidW_opt_vec     = nan(J,1);
eq_EbidW_opt_obj_vec = nan(J,1);
eq_Esales_opt_vec    = nan(J,1);
eq_Psales_opt_vec    = nan(J,1);
eq_Nall_opt_vec      = nan(J,1);
eq_EbidN_opt_vec     = nan(J,1);
eq_p_hcost0_opt_vec  = nan(J,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
for j = 1:J % loop over values of \sigma_{\psi}

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    [~,opt_ind]            = max(eq_Erev_mat(:,j)); % find value of alpha that maximizes seller surplus

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Store Auction Outcomes
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    q_opt_vec(j)            = q_vec(opt_ind); 
    eq_Erev_opt_vec(j)      = eq_Erev_mat(opt_ind,j);
    eq_EbidW_opt_vec(j)     = eq_EbidW_mat(opt_ind,j);
    eq_EbidW_opt_obj_vec(j) = eq_EbidW_obj_mat(opt_ind,j);    
    eq_Esales_opt_vec(j)    = eq_Esales_mat(opt_ind,j); 
    eq_Psales_opt_vec(j)    = eq_Psales_mat(opt_ind,j);
    eq_Nall_opt_vec(j)      = eq_Nall_mat(opt_ind,j);
    eq_EbidN_opt_vec(j)     = eq_EbidN_mat(opt_ind,j);
    eq_p_hcost0_opt_vec(j)  = eq_p_hcost0_mat(opt_ind,j);

end    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(6);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sel_x_obs   = (1:5)*2;
sel_y_obs   = 5;
n_y_obs     = size(sel_y_obs,2);
sel_y_chars = {'-b'};

subplot(3,2,1);
plot(endowM_grid(sel_x_obs),100*q_opt_vec(sel_x_obs),'-b','Linewidth',6); hold on;
ylabel('(%)');
xlabel('$\sigma_{\psi}$','interpreter','latex','fontsize',20); 
title('$\alpha^{*}$','interpreter','latex'); set(gca,'fontsize',FontSize); grid on; xlim([0.02,0.10]);

subplot(3,2,2);
plot(endowM_grid(sel_x_obs),eq_Erev_opt_vec(sel_x_obs)/1000,'-b','Linewidth',6); hold on;
ylabel('AUD 1,000');
xlabel('$\sigma_{\psi}$','interpreter','latex','fontsize',20);
title('E[Seller Revenue]'); set(gca,'fontsize',FontSize); grid on; xlim([0.02,0.10]);

subplot(3,2,3);
plot(endowM_grid(sel_x_obs),eq_EbidW_opt_vec(sel_x_obs)/1000,'-b','Linewidth',6); hold on;
ylabel('AUD 1,000');
xlabel('$\sigma_{\psi}$','interpreter','latex','fontsize',20);
title('E[Bidder Surplus] (Subjective)'); set(gca,'fontsize',FontSize); grid on; xlim([0.02,0.10]);

subplot(3,2,4);
plot(endowM_grid(sel_x_obs),eq_EbidW_opt_obj_vec(sel_x_obs)/1000,'-b','Linewidth',6); hold on;
ylabel('AUD 1,000');
xlabel('$\sigma_{\psi}$','interpreter','latex','fontsize',20);
title('E[Bidder Surplus] (Objective)'); set(gca,'fontsize',FontSize); grid on; xlim([0.02,0.10]);

subplot(3,2,5);
plot(endowM_grid(sel_x_obs),650+(eq_Esales_opt_vec(sel_x_obs)/1000),'-b','Linewidth',6); hold on;
ylabel('AUD 1,000');
xlabel('$\sigma_{\psi}$','interpreter','latex','fontsize',20);
title('E[Sales Price|Selling]'); set(gca,'fontsize',FontSize); grid on; xlim([0.02,0.10]);

subplot(3,2,6);
plot(endowM_grid(sel_x_obs),100*eq_Psales_opt_vec(sel_x_obs),'-b','Linewidth',6); hold on;
ylabel('(%)');
xlabel('$\sigma_{\psi}$','interpreter','latex','fontsize',20);
title('P[Sales]'); set(gca,'fontsize',FontSize); grid on; xlim([0.02,0.10]);

